/**
 * 
 */   
package com.gson.util;  

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.gson.model.Weather;
import com.gson.model.WeatherData;

/**   
 * @作者 Stone
 * @创建日期 2014-6-18 下午1:51:03
 * @版本 V1.0
 * @类说明
 */
public class WeatherHandler extends DefaultHandler {
	
	private boolean isWeather = false;
	private boolean xStatus = false;
	private boolean xDate = false;
	private boolean xResults = false;
	private boolean xCurrentCity = false;
	private boolean xWeatherData = false;
	private boolean xWeatherDate = false;
	private boolean xDayPictureUrl = false;
	private boolean xNightPictureUrl = false;
	private boolean xWeather = false;
	private boolean xWind = false;
	private boolean xTemperature = false;
	private Weather weatherInfo;
	private List<WeatherData> weatherDataList = new ArrayList<WeatherData>();
	private WeatherData data = null;
	
	public Weather parseWeather(InputStream is) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser;
		try {
			parser = factory.newSAXParser();
			WeatherHandler handler = new WeatherHandler();
			parser.parse (is,handler);
			return handler.getWeatherInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void startDocument() throws SAXException {
		weatherInfo = new Weather();
    }
	
	/**导航到开始标签触发**/
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) {
		String tagName = localName.length() == 0 ? qName : localName;
		tagName = tagName.trim();
		//如果读取的是weatherInfo标签开始，则实例化weatherInfo
		if ("CityWeatherResponse".equalsIgnoreCase(qName)) {
			isWeather = true;
			weatherInfo = new Weather();
		}
		//然后读取其他节点
		if (isWeather) {
			if (tagName.equals("status")) {
				xStatus = true;
			}
				
			if (!xWeatherData && tagName.equals("date")) {
				xDate = true;
			}
				
			if (tagName.equals("results")) {
				xResults = true;
			}
			
			if(xResults) {
				if (tagName.equals("currentCity")) {
					xCurrentCity = true;
				}
					
				if (tagName.equals("weather_data")) {
					xWeatherData = true;
				}
				
				if(xWeatherData) {
					if (tagName.equals("date")) {
						xWeatherDate = true;
						data = new WeatherData();
					}
					if (tagName.equals("dayPictureUrl")) {
						xDayPictureUrl = true;
					}
					if (tagName.equals("nightPictureUrl")) {
						xNightPictureUrl = true;
					}
					if (tagName.equals("weather")) {
						xWeather = true;
					}
					if (tagName.equals("wind")) {
						xWind = true;
					}
					if (tagName.equals("temperature")) {
						xTemperature = true;
					}
				}
					
			}
					
		}
	}
	
	/**导航到结束标签触发**/
    public void endElement(String uri, String localName, String qName)  
            throws SAXException {  
    	String tagName = localName.length() == 0 ? qName : localName;
    	//如果读取的是posInfo标签结束
    			
    			//然后读取其他节点
    			if (xWeatherData) {
    				if (tagName.equals("date")) {
						xWeatherDate = false;
					}
					if (tagName.equals("dayPictureUrl")) {
						xDayPictureUrl = false;
					}
					if (tagName.equals("nightPictureUrl")) {
						xNightPictureUrl = false;
					}
					if (tagName.equals("weather")) {
						xWeather = false;
					}
					if (tagName.equals("wind")) {
						xWind = false;
					}
					if (tagName.equals("temperature")) {
						xTemperature = false;
						weatherDataList.add(data);
					}
    			}
    			
    			if (tagName.equals("currentCity")) {
    				xCurrentCity = false;
    			}
    			
    			if(tagName.equals("weather_data")){
    				xWeatherData = false;
    			}
    			if(tagName.equals("results")){
    				xResults = false;
    			}
    			
    			if (!xWeatherData && tagName.equals("date")) {
    				xDate = false;
    			}
    			
    			if(tagName.equals("status")){
    				xStatus = false;
    			}
    			
    			if ("CityWeatherResponse".equalsIgnoreCase(tagName)) {
    				isWeather = false;
    				weatherInfo.setResults(weatherDataList);
    			} 
		
	}
	
	//这里是读取到节点内容时候回调
    public void characters(char[] ch, int start, int length) throws SAXException { 
    		String content = new String(ch,start,length);
    		if(content.indexOf("\n")>=0)
    			return;
    		// 设置属性值
    		if(xStatus){  
    			weatherInfo.setStatus(content);
            	return;
            }
    		if(xDate){  
    			weatherInfo.setDate(content);
            	return;
            }
    		if(xResults){
    			if(xCurrentCity){
    				weatherInfo.setCurrentCity(content);
    				return;
    			}
    			if(xWeatherData){
    				if(xWeatherDate){
    					data.setDate(content);
    					return;
    				}
    				if(xDayPictureUrl){
    					data.setDayPictureUrl(content);
    					return;
    				}
    				if(xNightPictureUrl){
    					data.setNightPictureUrl(content);
    					return;
    				}
    				if(xWeather){
    					data.setWeather(content);
    					return;
    				}
    				if(xWind){
    					data.setWind(content);
    					return;
    				}
    				if(xTemperature){
    					data.setTemperature(content);
    					return;
    				}
    			}
    		}
	}
	
	public Weather getWeatherInfo() {
		return weatherInfo;
	}

}
